import { getCateList,getTabChange,getDetail,isCollect,getCollect } from "@/services"

const state = {
    cateList:[],
    List:[],
    detail:{},
    showCollect:null,
    imgs:[]
}

const mutations = {
    update(state,payload) {
        for(let key in payload){
            state[key]=payload[key]
        }
    },
}

const actions = {
    // 所有的数据
    async getCate({commit}){
        let result =await getCateList()
        // console.log(result,'------------------------------');
        if(result){
            commit('update',{cateList:result})
        }
    },
    // tab切换的数据
    async getTabChange({commit},payload){
        let result = await getTabChange(payload)
        console.log(result.records);
        if(result){
            commit('update',{List:result.records})
        }
       
    },
    //跳详情
    async getDetail({commit},payload){
        // console.log(payload);
        let result= await getDetail(payload)
        // console.log(result)
        if(result){
            let imgs=result.imgs.split(',')
            commit('update',{
                detail:result,
                imgs:imgs
            })
        } 
       
    },
    // 判断是否收藏
    async isCollect({commit},payload){
        //console.log(payload);
        let result=await isCollect(payload)
        console.log(result);
        if(result){
            commit('update',{showCollect:result})        
            // console.log(state.showCollect);
        }

    },
    // 收藏
    async getCollect({commit},payload){
        let result=await getCollect(payload)
        commit('update',{showCollect:!state.showCollect})
        console.log(state.showCollect,'收藏');
    }

}

export default {
    namespaced: true,
    state,
    mutations,
    actions,
}